package com.dyz.leetcode;

import java.util.HashMap;

public class LengthOfLongestSubstring3 {
    public int lengthOfLongestSubstring(String s) {
        if(s==null||s.length()==0){return 0;}

        HashMap<Character, Integer> map = new HashMap<>();
        char[] ch = s.toCharArray();
        int res =0;//记录结果值
        int leftIndex = -1;//左边标记
        //方法是i指针固定走， leftIndex根据是否重复变换位置
        for(int i= 0; i<ch.length;i++){
            if(map.containsKey(ch[i])){
                leftIndex = Math.max(leftIndex, map.get(ch[i]));
            }
            map.put(ch[i], i);
            res = Math.max(res, i-leftIndex);
        }
        return res;
    }
}
